package com.hdu.other.dp.treeDP;

public class lc337 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public int rob(TreeNode root)
        {
           Info info = f(root);
           return Math.max(info.no, info.yes);
        }

        Info f(TreeNode x) {
            if (x == null) {
                return new Info(0, 0);
            }
            Info left = f(x.left);
            Info right = f(x.right);
            int no = Math.max(left.no, left.yes) + Math.max(right.no, right.yes);
            int yes = x.val + left.no + right.no;
            return new Info(yes, no);
        }

        class Info
        {
            int yes;
            int no;

            public Info(int yes, int no)
            {
                this.yes = yes;
                this.no = no;
            }
        }

    }
}
